var wikimapiaCategoryDao = require('../daos/wikimapiaCategoryDao'),
	idService = require('./idService'),
	async = require("async");

// -------------------------- CRUD SERVICES ----------------------------

module.exports.getAll = function (short, sortField, sortOrd, callback) {
	wikimapiaCategoryDao.getAll(short, sortField, sortOrd, callback);
}

module.exports.getAllLanguageNames = function (language, callback) {
	var wikimapiaCategories = [];
	if (language.match('^ES.*')) {
		wikimapiaCategoryDao.getAll(false, 'title_es', 1, function(err, data) {
			if (err) callback(err);
			else {
				async.forEachSeries(data, function(datum, callback2) {
					wikimapiaCategories.push(datum.title_es);
					callback2();
				}, function(err) {
		            if (err) callback("ERROR EN forEachSeries" + err);
		            else callback(false, wikimapiaCategories);
		        });
			}
		});
	}
}

module.exports.find = function (filters, callback) {
	wikimapiaCategoryDao.find(filters, callback);
}

module.exports.findOne = function (filters, callback) {
	wikimapiaCategoryDao.findOne(filters, callback);
}

module.exports.save = function (category, callback) {
	if (!category.id) {
		console.log("La categoría de Wikimapia debe tener ID");
	} else {
		wikimapiaCategoryDao.upsert(category.id, category, function(err) {
			if (err)
				callback(err);
			else
				callback(false, category);
		});
	}
}

module.exports.remove = function (id, callback) {
	wikimapiaCategoryDao.remove(id, callback);
}

module.exports.drop = function () {
	return wikimapiaCategoryDao.drop();
}
